
package org.example.websocket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * Websocket controller is broadcasting every {fixedDelay = 1000} ms the data through websockets 

 * @author jasrodis
 */
@Controller
public class WebSocketController {

	@Autowired
	private ObjectMapper mapper;
	@Autowired
	private SimpMessagingTemplate messageTemplate;

	@MessageMapping("/topic/chat")
	public void chat(Message message) throws JsonProcessingException {
		this.messageTemplate.convertAndSend("/topic/chat",
				mapper.writeValueAsString(message)
		);
	}

}
